package com.linkivr;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Properties;


public class Configuration {
	private Properties pro;
	//private FileInputStream inputFile;
	private FileOutputStream outputFile;
	private boolean ready = false;

	public Configuration() {
		pro = new Properties();
	}


	public Configuration(InputStream  inputFile) {
		pro = new Properties();
		try {


			//inputFile = new FileInputStream(filePath);this.getClass().getResourceAsStream("/resource/res.txt");   
			// inputFile =;
			pro.load(inputFile);
			inputFile.close();
			ready = true;
		} catch (FileNotFoundException e) {
			GL.error("系统找不到指定文件");
			GL.logException(e);
		} catch (IOException e) {
			GL.error("读取文件失败!");
			GL.logException(e);
		}
	}

	public boolean getReady() {
		return ready;
	}


	public String getValue(String key) {
		if (pro.containsKey(key)) {
			String value = pro.getProperty(key).trim();
			String value2 = null;
			try {
				value2 = new String(value.getBytes("ISO-8859-1"), "UTF-8");
			} catch (UnsupportedEncodingException e) {
				GL.logException(e);
			}

			return value2;
		} else {
			return "";
		}
	}


	public int getIntValue(String key) {
		int result = 0;
		if (pro.containsKey(key)) {
			String value = pro.getProperty(key).trim();
			try {
				result = Integer.parseInt(value);
				;
			} catch (NumberFormatException e) {
				GL.logException(e);
			}
		}
		return result;
	}

	public void clear() {
		pro.clear();
	}

	public void setValue(String key, String value) {
		pro.setProperty(key, value);
	}


	public void saveFile(String fileName, String description) {
		try {
			outputFile = new FileOutputStream(fileName);
			pro.store(outputFile, description);
			outputFile.close();
		} catch (Exception e) {
			GL.logException(e);
		}
	}

	/*
	 * public static void main(String[] args) {
	 * 
	 * Configuration conf = new Configuration("./bin/appconf.properties");
	 * String id = conf.getValue("app.id"); System.out.println("<" + id + ">");
	 * String name = conf.getValue("app.name");
	 * 
	 * // int port = Integer.parseInt(sport);// System.out.println(name); }
	 */
}
